package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootBfinfo;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.domain.enums.BFZT;
import org.footballmanager.pandafootball.system.form.impl.FootBfinfoSearchForm;
import org.footballmanager.pandafootball.system.service.FootBfinfoService;
import org.footballmanager.pandafootball.system.service.FootSjService;
import org.footballmanager.pandafootball.system.service.FootUserPersonService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@SessionAttributes("footBfinfoSearchForm")
@RequestMapping("/hy/footBfinfo")
public class HyBfinfoController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footBfinfo";

	@Autowired
	private FootBfinfoService footBfinfoService;
	@Autowired
	private FootUserPersonService footUserPersonService;
	@Autowired
	private FootUserService footUserService;
	@Autowired
	private FootSjService footSjService;

    /**
     * 初始化
     * @param map 参数
     * @param footBfinfoSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm){
        map.addAttribute("sjList", footSjService.getall());
        FootBfinfoSearchForm newFootBfinfoSearchForm = new FootBfinfoSearchForm();
        if(footBfinfoSearchForm != null){
            newFootBfinfoSearchForm = footBfinfoSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootBfinfoSearchForm);
    }

	@RequestMapping(value="/search")
	public String search(Model map, Integer type, @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        footBfinfoSearchForm.setBflx(BFLX.values()[type]);
        if(!type.equals(0)){
            footBfinfoSearchForm.setSj(footSjService.getSj());
        }else{
            footBfinfoSearchForm.setSj(null);
        }
        footBfinfoService.search(ENTITYNAME, map, footBfinfoSearchForm);
        return footBfinfoService.searchPage(footBfinfoSearchForm, true);
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String openAddPage(Model map, HttpServletRequest request, @RequestParam Integer type) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
        return footBfinfoService.add(user, map, type, request);
	}

	@RequestMapping(value = "/searchUser", method = RequestMethod.POST)
	@ResponseBody
	public List<FootUser> searchUser(HttpServletRequest request) {
        return footUserService.getAllWithoutAdmin(request);
	}
	
	@RequestMapping(value = "/searchPerson", method = RequestMethod.POST)
	@ResponseBody
	public List<FootPerson> searchPerson(@RequestParam Long userId, HttpServletRequest request) {
		return footUserPersonService.findFootPersonByUserId(userId, request);
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String save(@Valid @ModelAttribute FootBfinfo footBfinfo,
			BindingResult bindingResult, HttpServletRequest request, Model map, SessionStatus s) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
        footBfinfoService.processError(footBfinfo, bindingResult, request);
		if(!bindingResult.hasErrors()){
            footBfinfoService.save(footBfinfo, request);
            s.setComplete();
			return "redirect:/hy/footBfinfo/view?id=" + footBfinfo.getId();
		}else{
            return footBfinfoService.processReturnPage(footBfinfo, map, user, request);
		}
	}

	@RequestMapping(value="/mySearch")
	public String mySearch(Model map, Integer type, HttpServletRequest req,
                             @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
		FootUser user = (FootUser)req.getSession().getAttribute(Constants.USER);
        footBfinfoSearchForm.setBflx(BFLX.values()[type]);
        if(!type.equals(0)){
            footBfinfoSearchForm.setSj(footSjService.getSj());
        }else{
            footBfinfoSearchForm.setSj(null);
        }
        footBfinfoSearchForm.setBfqd(user.getId());
        footBfinfoSearchForm.setBfzt(BFZT.NOT);
        footBfinfoService.search(ENTITYNAME, map, footBfinfoSearchForm);
        return footBfinfoService.searchPage(footBfinfoSearchForm, false);
	}
	
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                       @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        return footBfinfoService.view(id, map, request, footBfinfoSearchForm, true);
	}
	
	@RequestMapping(value = "/myView", method = RequestMethod.GET)
	public String openMyViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        return footBfinfoService.view(id, map, request, footBfinfoSearchForm, false);
	}
	
	@RequestMapping(value = "/makesure", method = RequestMethod.POST)
	public String makesure(@ModelAttribute FootBfinfo footBfinfo, HttpServletRequest request) {
        footBfinfoService.makesure(footBfinfo, request);
		return "redirect:/hy/footBfinfo/view";
	}

}
